package com.ogawa.project6602.activity

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.ogawa.base.utils.HexUtil
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.project6602.R
import com.ogawa.project6602.base.Base6602MqttActivity
import com.ogawa.project6602.bean.Massage6602Armchair

@Route(path = UrlPathConstant.ACTIVITY_6602_ADVANCE)
class Device6602AdvanceActivity : Base6602MqttActivity(), View.OnClickListener {
    private lateinit var tvJingjian: TextView
    private lateinit var tvShoubu: TextView
    private lateinit var tvTunbu: TextView
    private lateinit var tvJiaobu: TextView
    private lateinit var tvYaobei: TextView
    private var datas = byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0)
    override fun getLayoutId(): Int {
        return R.layout.activity_6602_advance
    }

    override fun initView() {
        super.initView()
        setTitle(getString(R.string.advance_massage))
        tvJingjian = findViewById(R.id.tv_jingjian)
        tvJingjian.setOnClickListener(this)
        tvShoubu = findViewById(R.id.tv_shoubu)
        tvShoubu.setOnClickListener(this)
        tvTunbu = findViewById(R.id.tv_tunbu)
        tvTunbu.setOnClickListener(this)
        tvJiaobu = findViewById(R.id.tv_tuibu)
        tvJiaobu.setOnClickListener(this)
        tvYaobei = findViewById(R.id.tv_yaobei)
        tvYaobei.setOnClickListener(this)
        findViewById<TextView>(R.id.tv_cancel).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_confirm).setOnClickListener(this)
        setProgram()
    }

    private fun setProgram() {
        val deviceStatusBean = Massage6602Armchair.getInstance().deviceStatusBean
        if (deviceStatusBean != null) {
            val advance = deviceStatusBean.functions.advanceAutoProgram.modelValue.toInt()
            tvJingjian.isSelected = advance.ushr(0).and(1) ==1
            tvShoubu.isSelected =advance.ushr(1).and(1) ==1
            tvTunbu.isSelected = advance.ushr(2).and(1) ==1
            tvJiaobu.isSelected = advance.ushr(3).and(1) ==1
            tvYaobei.isSelected = advance.ushr(4).and(1) ==1
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_jingjian,
            R.id.tv_shoubu,
            R.id.tv_tunbu,
            R.id.tv_tuibu,
            R.id.tv_yaobei -> v.isSelected = !v.isSelected
            R.id.tv_cancel -> {
                tvJingjian.isSelected = false
                tvShoubu.isSelected = false
                tvTunbu.isSelected = false
                tvJiaobu.isSelected = false
                tvYaobei.isSelected = false
                finish()
            }
            R.id.tv_confirm -> {
                datas[7] = if (tvJingjian.isSelected) 1 else 0
                datas[6] = if (tvShoubu.isSelected) 1 else 0
                datas[5] = if (tvTunbu.isSelected) 1 else 0
                datas[4] = if (tvJiaobu.isSelected) 1 else 0
                datas[3] = if (tvYaobei.isSelected) 1 else 0
                val program = HexUtil.hexToInt(
                    HexUtil.bitToHex(datas)
                )
                if (program != 0) {
                    setResult(Activity.RESULT_OK, Intent().putExtra("program", program.toString()))
                }
                finish()
            }
        }
    }

}